﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ServiceBase;
namespace ServicePrice
{
    public class www_360buy_com : IServicePrice
    {
        private Dictionary<string, char> _matrix = new Dictionary<string, char>();
        public www_360buy_com()
        {
            _matrix.Add("111101111011100110001110100001110100000111000000111000001111100000111000000111100", '￥');
            _matrix.Add("001110011000110000111110110011110011110011011110", '6');
            _matrix.Add("011111011000011000011110000011000011110011011110", '5');
            _matrix.Add("011110110011110011110011011111000011000110011100", '9');
            _matrix.Add("1111", '.');
            _matrix.Add("011110110011110011110011110011110011110011011110", '0');
            _matrix.Add("01101110011001100110011001101111", '1');
            _matrix.Add("011110110011110011011110110011110011110011011110", '8');
            _matrix.Add("000010000110001110010110100110111111000110000110", '4');
            _matrix.Add("011110110011000011000110001100011000110000111111", '2');
            _matrix.Add("111111000011000110000110001100001100011000011000", '7');
            _matrix.Add("011110110011000011001110000011000011110011011110", '3');
        }
        /// <summary>
        /// 识别一张图片价格，并返回图片上的值。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string DoRecognition(string url)
        {
            //下载图片
            Bitmap map = GetBitmap.From_URL(url);
            //处理成可识别的图片
            Bitmap[] list = Dispose(map);
            //识别
            string result = Match.Match_Dictionary.ByDictionary(list, _matrix);
            return result;
        }

        private Bitmap[] Dispose(Bitmap map)
        {
            //二值化
            SimpleColor.TwoColor1(map);
            //按空列切割
            Bitmap[] list = Split.ByEmptyColumn(map);
            //切割粘连片段
            //list = Split.ByLimitWidth(list, 11, 10);
            return list;
        }

        public Bitmap[] ViewMatrix(string dir)
        {
            Bitmap[] sourceList = GetBitmap.From_Folder(dir);
            List<Bitmap> all = new List<Bitmap>();
            foreach (Bitmap map in sourceList)
            {
                Bitmap[] list = Dispose(map);
                all.AddRange(list);
            }
            Bitmap[] matrix = ServiceMatrix.Pick_Matrix.ByBitmapList(all.ToArray());
            Helper_View.View.View_Bitmap(matrix);
            return matrix;
        }

        public void CreateMatrix(string dir)
        {
            Bitmap[] matrix = ViewMatrix(dir);
            ServiceMatrix.Builder_Matrix.Create(matrix, Guid.NewGuid().ToString() + ".txt", ServiceMatrix.Builder_Matrix.MatrixType.零一字符串);
        }
    }
}
